package com.example.flashsales.service.impl;

import com.example.flashsales.entity.pojo.StoreCostRecord;
import com.example.flashsales.entity.vo.StoreCostRecordVo;
import com.example.flashsales.mappers.StoreCostRecordMapper;
import com.example.flashsales.service.StoreCostRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class StoreCostRecordServiceImpl implements StoreCostRecordService {

    @Autowired
    StoreCostRecordMapper storeCostRecordMapper;

    @Autowired
    StoreServiceImpl storeService;


    /**
     * 增加一条纪录
     * @param storeId 店家id
     * @param money 金钱
     * @param state 类型
     * @return 返回是否添加成功
     */
    @Override
    public boolean addCostRecord(long storeId, double money, int state) {
        final Map<String, Double> map = storeService.viewMoney(storeId);
        if (map == null){
            return false;
        }
        double v = map.get("money");
        double sMoney = v + money;
        return storeCostRecordMapper.addCostRecord(storeId, money, state, sMoney) > 0;
    }

    /**
     * 查看所有的流水纪录
     * @param storeId 店家id
     * @param page 页数
     * @param num 条数
     * @return 返回流水视图对象集合 */
    @Override
    public List<StoreCostRecordVo> viewAllStoreCostRecord(long storeId, int page, int num) {
        if (page <= 0){
            return null;
        }
        page = (page - 1)*num;
        final List<StoreCostRecord> storeCostRecords = storeCostRecordMapper.viewAllStoreCostRecord(storeId, page, num);
        return tools(storeCostRecords);
    }


    /**
     * 按时间类型查看流水纪录
     * @param storeId 店家id
     * @param qDate 前一天
     * @param hDate 后一天
     * @param page 页数
     * @param num 条数
     * @param state 类型 -1 全部，0 提现，1拉人，2取货
     * @return 返回流水视图对象集合
     */
    @Override
    public List<StoreCostRecordVo> viewStoreRecordByTimeAndState(long storeId, Date qDate, Date hDate, int page, int num, int state) {
        if (page < 0){
            return null;
        }
        page = (page - 1)*num;
        final List<StoreCostRecord> storeCostRecords;
        if (state == -1){
            storeCostRecords = storeCostRecordMapper.viewStoreRecordByTime(storeId, qDate, hDate, page, num);
        }else {
            storeCostRecords = storeCostRecordMapper.viewStoreRecordByTimeAndState(storeId, qDate, hDate, page, num, state);
        }
        return tools(storeCostRecords);
    }

    /**
     * 查看总条数
     * @param storeId
     * @return
     */
    @Override
    public long viewCount(long storeId) {
        return storeCostRecordMapper.viewCount(storeId);
    }


    /**
     * 按时间类型查看条数
     * @param id
     * @param qDate
     * @param hDate
     * @param state
     * @return
     */
    @Override
    public long viewCountByTime(long id,Date qDate,Date hDate,int state) {
        return storeCostRecordMapper.viewCountByTime(id,qDate,hDate,state);
    }

    /**
     * 视图封装工具
     * @param storeCostRecords
     * @return
     */
    private List<StoreCostRecordVo> tools(List<StoreCostRecord> storeCostRecords){
        List<StoreCostRecordVo> storeCostRecordVos = new ArrayList<>();
        for (StoreCostRecord storeCostRecord :storeCostRecords) {
            StoreCostRecordVo storeCostRecordVo = new StoreCostRecordVo();
            BeanUtils.copyProperties(storeCostRecord,storeCostRecordVo);
            if (storeCostRecordVo.getState() == 0){
                storeCostRecordVo.setSState("提现");
            }else if (storeCostRecordVo.getState() == 1){
                storeCostRecordVo.setSState("拉人");
            }else if (storeCostRecordVo.getState() == 2){
                storeCostRecordVo.setSState("订单");
            }else {
                storeCostRecordVo.setSState("未知");
            }
            storeCostRecordVos.add(storeCostRecordVo);
        }

        return storeCostRecordVos;
    }
}
